package com.gxuwz.com_property.mail.controller;


import com.gxuwz.com_property.common.dto.ResponseDto;
import com.gxuwz.com_property.mail.pojo.Mail;
import com.gxuwz.com_property.mail.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.Optional;
import java.util.stream.Stream;

@RestController
public class MailController {

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private MailService mailService;


    @GetMapping("/mail")
    public String senderMail() {
        SimpleMailMessage message = new SimpleMailMessage();
        // 发件人 你的邮箱
        message.setFrom("1784121179@qq.com");
        // 接收人 接收者邮箱
        message.setTo(new String[]{"1784121179@qq.com"});

        //邮件标题
        message.setSubject("hello");

        //邮件内容
        message.setText("world");
        javaMailSender.send(message);

        return "success";
    }

    private static int count1;

    // 定义一个线程安全的日期格式化对象
    private static final SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @PostMapping("/sendMail")
    //@Scheduled(fixedRate = 30000)
    public ResponseDto sendUserEmail(@RequestBody Mail mail) {

        System.out.println("mail"+mail);
        long id = mail.getId();
        String name = mail.getName();
        String eamil = mail.getEmail();
        String address = mail.getAddress();
        String feeType = mail.getFeeType();
        String startDate = mail.getStartDate();
        String endDate = mail.getEndDate();
        String amount = mail.getAmount();
        String status = mail.getStatus();

        // 设置邮件发送所需的参数
        String from = "1784121179@qq.com";
        String to = eamil;
        String subject = "缴费通知";
        // 构造邮件正文内容
        String text = "尊敬的" + name + "业主：\n\n"
                + "您好！"+address+"我们发现您的"
                + "- 缴费项目：" + feeType + "\n"
                + "- 费用金额：" + amount + " 元\n"
                +"- 处于：" + status + " 状态\n"
                + "- 计费开始时间为：" + startDate + "\n\n"
                + "- 计费结束时间为：" + endDate + "\n\n"
                + "请您尽快进行缴纳相关费用，以免影响您的居住权益。\n\n"
                + "如有任何问题，请随时与我们联系。谢谢！\n\n";


        System.out.println(String.format("fixedRate第%s次执行发送邮件数据，当前时间为：%s", count1++, dataFormat.format(new Date())));

        // 使用MailService类的sendEmailWithAttachmentAndInlineResource方法发送带有最新.sql文件附件的邮件
        mailService.sendComplexEmail(from, to, subject, text, "", null, null);
        return ResponseDto.success("邮件发送成功");

    }


}
